<template>
  <div id="app">
    <router-view />
  </div>
</template>

<script>
export default {
  created() {
    // 在页面加载时读取sessionStorage里的状态信息
    // if (typeof(sessionStorage.getItem('storeState')) !== 'undefined') {
    if (sessionStorage.getItem("storeState") != null) {
      // replaceState，替换store的根状态
      this.$store.replaceState(
        Object.assign(
          {},
          this.$store.state,
          sessionStorage.getItem("storeState")
        )
      );
    }
    if (sessionStorage.getItem("user") != null) {
      // replaceState，替换store的根状态
      this.$store.commit("setUser", JSON.parse(sessionStorage.getItem("user")));
    }

    if (sessionStorage.getItem("tag") != null) {
      // replaceState，替换store的根状态
      this.$store.state.stateTagsList = JSON.parse(
        sessionStorage.getItem("tag")
      );
    }

    // 在页面刷新时将vuex里的信息保存到sessionStorage里
    window.addEventListener("beforeunload", () => {
      sessionStorage.setItem("storeState", this.$store.state);
      sessionStorage.setItem(
        "tag",
        JSON.stringify(this.$store.state.stateTagsList)
      );
      sessionStorage.setItem("user", JSON.stringify(this.$store.state.user));
    });
  },
};
</script>

<style lang="less">
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  color: #2c3e50;
}

nav {
  padding: 30px;

  a {
    font-weight: bold;
    color: #2c3e50;

    &.router-link-exact-active {
      color: #2cd3b5;
    }
  }
}
::-webkit-scrollbar {
  -webkit-appearance: none;
  width: 6px;
  height: 6px;
}
::-webkit-scrollbar-track {
  background: #ffff;
  border-radius: 0;
}
::-webkit-scrollbar-thumb {
  cursor: pointer;
  border-radius: 15px;
  background: #bcf0e7;
  transition: color 0.2s ease;
}
::-webkit-scrollbar-thumb:hover {
  background: #2cd3b5;
}

</style>
